package com.lxg.controller.adminController;

import com.lxg.pojo.*;
import com.lxg.service.adminService.AdminAdminService;
import com.lxg.service.adminService.AdminBooksService;
import com.lxg.service.adminService.AdminProblemService;
import com.lxg.service.adminService.AdminUserService;
import com.lxg.service.userService.UserBorrowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.awt.print.Book;
import java.util.ArrayList;

@Controller
public class AdminController {

    @Autowired
    AdminAdminService adminAdminService;
    @Autowired
    AdminBooksService adminBooksService;
    @Autowired
    AdminProblemService adminProblemService;
    @Autowired
    AdminUserService adminUserService;

    @Autowired
    UserBorrowService userBorrowService;

    /**
     * 获取全部管理员的信息
     *
     * @return
     */
    @RequestMapping("/getAllAdmin")
    public String getAllAdmin(HttpSession session){
        ArrayList<Admin> allAdmin = adminAdminService.getAllAdmin();
        session.setAttribute("allAdmin",allAdmin);
        return "/index";
    }

    /**
     * 根据传入的 aid，查找对应管理员
     *
     * @param aid
     * @return
     */
    @RequestMapping("/getAdminByAid")
    public String getAdminByAid(Integer aid , HttpSession session){
        Admin adminByAid = adminAdminService.getAdminByAid(aid);
        session.setAttribute("adminByAid",adminByAid);
        return "/index";
    }

    @RequestMapping("/deleteAdminByAid")
    public String deleteAdminByAid(Integer aid , HttpSession session){
        boolean b = adminAdminService.deleteAdminByAid(aid);
        session.setAttribute("result",b);
        return "/index";
    }





    //添加用户
    @RequestMapping("/admin/addUser")
    public String addUser(HttpServletRequest request,User user, HttpSession session){
        if(user.getPhone()=="" ||user.getName()=="" ||user.getGrade()=="" || user.getUsername()=="" || user.getEmail()=="" || user.getSno()==""){
            boolean addResult=false;
            return "redirect:/admin/userManage?addResult="+addResult;
        }else{
            boolean b = adminUserService.addUser(user);
            boolean addResult=b;
            return "redirect:/admin/userManage?addResult="+addResult;
        }

    }


    //删除用户
    @RequestMapping("/admin/deleteUser")
    public String deleteUser(HttpServletRequest request,User user, HttpSession session){
        Integer uid = Integer.valueOf(request.getParameter("uid"));
        System.out.println(uid);
        boolean b = adminUserService.deleteUserByUid(uid);
        return "redirect:/admin/userManage";
    }


    //修改用户
    @RequestMapping("/admin/updateUser")
    public String updateUser(HttpServletRequest request,User user, HttpSession session){
        if(user.getPhone()=="" ||user.getName()=="" ||user.getGrade()=="" || user.getUsername()=="" || user.getEmail()=="" || user.getSno()==""){
            boolean updateResult=false;
            return "redirect:/admin/userManage?updateResult="+updateResult;
        }else{
            Integer uid = user.getUid();
            String username = user.getUsername();
            String password = user.getPassword();
            String email = user.getEmail();
            String name = user.getName();
            String sno = user.getSno();
            String grade = user.getGrade();
            String phone = user.getPhone();


            boolean b = adminUserService.updateUser(username, password, name, sno, grade, email, phone,uid);
            boolean updateResult=b;
            return "redirect:/admin/userManage?updateResult="+updateResult;
        }

    }


    //添加管理员
    @RequestMapping("/admin/addAdmin")
    public String addAdmin(HttpServletRequest request,Admin admin, HttpSession session){
        if(admin.getUsername()=="" || admin.getPassword()==""){
            boolean addResult=false;
            return "redirect:/admin/adminManage?addResult="+addResult;
        }else{
            boolean b = adminAdminService.addAdmin(admin.getUsername(),admin.getPassword());
            boolean addResult=b;
            return "redirect:/admin/adminManage?addResult="+addResult;
        }

    }


    //删除管理员
    @RequestMapping("/admin/deleteAdmin")
    public String deleteAdmin(HttpServletRequest request,User user, HttpSession session){
        Integer aid = Integer.valueOf(request.getParameter("aid"));
        System.out.println(aid);
        boolean b = adminAdminService.deleteAdminByAid(aid);
        return "redirect:/admin/adminManage";
    }


    //修改管理员
    @RequestMapping("/admin/updateAdmin")
    public String updateAdmin(HttpServletRequest request,Admin admin, HttpSession session){
        if(admin.getUsername()=="" || admin.getPassword()==""){
            boolean updateResult=false;
            return "redirect:/admin/userManage?updateResult="+updateResult;
        }else{

            boolean b = adminAdminService.updateAdmin(admin.getUsername(), admin.getPassword(), admin.getAid());
            boolean updateResult=b;
            return "redirect:/admin/userManage?updateResult="+updateResult;
        }

    }


   //添加图书
    @RequestMapping("/admin/addBook")
    public String addBook(Books book, HttpSession session){
        if(book.getName()=="" || book.getNum()==null || book.getAuthor()=="" || book.getAddress()=="" || book.getPress()=="" || book.getRemainder()==null){
            boolean addResult=false;
            return "redirect:/admin/bookManage?addResult="+addResult;
        }else{
            String name = book.getName();
            Integer num = book.getNum();
            String address = book.getAddress();
            Integer remainder = book.getRemainder();
            String author = book.getAuthor();
            String press = book.getPress();
            boolean b = adminBooksService.addBook(name, author, num, remainder, press, address);
            boolean addResult=b;
            return "redirect:/admin/bookManage?addResult="+addResult;
        }
    }


    //删除图书
    @RequestMapping("/admin/deleteBook")
    public String deleteBookByBid(HttpServletRequest request,HttpSession session){
        Integer bid = Integer.valueOf(request.getParameter("bid"));
        boolean b = adminBooksService.deleteBookByBid(bid);
        return "redirect:/admin/bookManage";
    }

    //更新图书
    @RequestMapping("/updateBook")
    public String updateBook(Books book,HttpSession session){


        if(book.getName()=="" || book.getNum()==null || book.getAuthor()=="" || book.getAddress()=="" || book.getPress()=="" || book.getRemainder()==null){
            boolean updateResult=false;
            return "redirect:/admin/bookManage?updateResult="+updateResult;
        }else{
            String name = book.getName();
            Integer num = book.getNum();
            String address = book.getAddress();
            Integer remainder = book.getRemainder();
            String author = book.getAuthor();
            String press = book.getPress();
            Integer bid = book.getBid();
            boolean b = adminBooksService.adminUpdateBook(bid, name, num, remainder, author, address, press);
            boolean updateResult=b;
            return "redirect:/admin/bookManage?updateResult="+updateResult;
        }
    }




    /**
     * 查询未解决的问题
     * @return
     */
    @RequestMapping("/getAllUnsolved")
    public String getAllUnsolved(HttpSession session){
        ArrayList<Problem> allUnsolved = adminProblemService.getAllUnsolved();
        session.setAttribute("allUnsolved",allUnsolved);
        return "/index";
    }


    /**
     * 查询已经解决的问题
     * @return
     */
    @RequestMapping("/getAllSolved")
    public String  getAllSolved(HttpSession session){
        ArrayList<Problem> allSolved = adminProblemService.getAllSolved();
        session.setAttribute("allSolved",allSolved);
        return "/index";
    }


    /**
     * 修改问题的状态，改为已解决
     *
     */
    @RequestMapping("/admin/changeStatus")
    public String updateProblem( HttpServletRequest request,HttpSession session){
        String pid = request.getParameter("pid");
        boolean b = adminProblemService.updateProblem(Integer.valueOf(pid));
        session.setAttribute("result",b);
        return "redirect:/admin/problemManage";
    }



    /**
     * 根据uid删除一个用户
     * @param uid
     */
    @RequestMapping("/deleteUserByUid")
    public String deleteUserByUid(Integer uid,HttpSession session){
        boolean b = adminUserService.deleteUserByUid(uid);
        session.setAttribute("result",b);
        return "/index";
    }


    //批准借阅请求
    @RequestMapping("/admin/agreeBorrow")
    public String agreeBorrow(HttpServletRequest request,HttpSession session){
        //获取uid和bid
        Integer bid = Integer.valueOf(request.getParameter("bid"));
        Integer uid = Integer.valueOf(request.getParameter("uid"));

        //获取当前借阅状态
        Integer historyStatus = userBorrowService.getHistoryStatus(uid,bid);
        if(historyStatus!=0){
            return  "redirect:/admin/approval?result="+false;
        }
        //修改借阅记录状态
        boolean b  = userBorrowService.updateHistoryStatus(uid, bid);

        Borrow borrow = userBorrowService.getBorrow(bid, uid);
        if(borrow != null){
            boolean b1 = userBorrowService.updateBorrowStatus(bid, uid);
            return  "redirect:/admin/approval?agreeResult="+b1;
        }else {
            return  "redirect:/admin/approval?agreeResult="+false;
        }
    }


    //拒绝借阅请求
    @RequestMapping("/admin/refuseBorrow")
    public String refuseBorrow(HttpServletRequest request){

        //获取uid和bid
        Integer bid = Integer.valueOf(request.getParameter("bid"));
        Integer uid = Integer.valueOf(request.getParameter("uid"));
        //直接删除借阅记录
        boolean b = userBorrowService.deleteHistoryByUidAndBid(uid, bid);
        return  "redirect:/admin/approval?deleteResult="+b;
    }

}
